package com.statistics.export;


import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfFont;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 导出任务书word
 */
public class ExportTaskBookWordDemo {

    private final static Logger logger = LoggerFactory.getLogger(ExportTaskBookWordDemo.class);

    //小数格式化
    private static DecimalFormat df = new DecimalFormat("###.######");

    private Pattern pattern5 = Pattern.compile("/");

    /**
     * 合同导出word
     *
     * @param uploadFolder
     * @param
     * @return
     */
    public String getContractWordList(String uploadFolder) {

        DateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
        //date类型转换成字符 当前时间
        String nowDate = sdf2.format(new Date());
        uploadFolder = uploadFolder + System.currentTimeMillis() + "/";
        String pathName = uploadFolder + "测试word" + nowDate;
        File f1 = new File(pathName);
        //验证这个文件是否存在
        if (!f1.exists()) {
            //不存在就创建
            f1.mkdirs();
        }
        // 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        try {

            // 设置第一页
            RtfWriter2.getInstance(document, new FileOutputStream(pathName + ".docx"));
            document.open();
            //章节二
            Chapter chapter2 = new Chapter(1);;
            home(document);
            for (int i = 0; i <4; i++) {
                // 基本信息
                basicInfo(chapter2);
                //请求表格
                RequestTable(chapter2);
                //返回表格
                ResponseTable(chapter2);
                //返回示例
                example(chapter2);
            }
            document.add(chapter2);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pathName + ".docx";

    }

   /**
    *@author: hewei
    *@Description: 设置word第一页
    */
    public void home(Document document) throws DocumentException {

        //章节一
        Chapter chapter1 = new Chapter(1);
        //设置合同头
        Paragraph p = new Paragraph("\n\n能科MES api文档\n\n", new RtfFont("华文中宋", 26, Font.BOLD, Color.BLACK));
        p.setAlignment(1);
        chapter1.add(p);
        //接口说明文档
        Paragraph p1 = new Paragraph("接口说明文档", new RtfFont("华文中宋", 15, Font.NORMAL,Color.BLACK));
        p1.setAlignment(1);
        //跳到页低
        Paragraph p31 = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n\n\n", new RtfFont("华文中宋", 18, Font.BOLD, Color.BLACK));
        //导出时间
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Paragraph p2 = new Paragraph(df.format(LocalDate.now()), new RtfFont("华文中宋", 15, Font.NORMAL,Color.BLACK));
        p2.setAlignment(1);
        // 下划线
        Paragraph p32 = new Paragraph("------------------------------------------------------------------------"
                , new RtfFont("华文中宋", 18, Font.BOLD, Color.BLACK));
        chapter1.add(p1);
        chapter1.add(p31);
        chapter1.add(p2);
        chapter1.add(p32);
        document.add(chapter1);

    }

    /**
     *@author: hewei
     *@Description: 设置基本信息
     */
    public Chapter basicInfo(Chapter chapter2) throws DocumentException {

        //接口名称
        Paragraph p3 = new Paragraph("\n接口名称:\n", new RtfFont("宋体", 18, Font.BOLD, Color.BLACK));
        //基本信息
        Paragraph p4 = new Paragraph("\n基本信息\n", new RtfFont("宋体", 16, Font.BOLD, Color.BLACK));
        //接口地址
        Paragraph p5 = new Paragraph("\n接口地址\n", new RtfFont("宋体", 12, Font.NORMAL, Color.BLACK));
        //返回值类型
        Paragraph p6 = new Paragraph("\n返回值类型\n", new RtfFont("宋体", 12, Font.NORMAL, Color.BLACK));
        //请求方式
        Paragraph p7 = new Paragraph("\n请求方式\n", new RtfFont("宋体", 12, Font.NORMAL, Color.BLACK));
        //备注
        Paragraph p8 = new Paragraph("\n备注\n", new RtfFont("宋体", 12, Font.NORMAL, Color.BLACK));
        chapter2.add(p3);
        chapter2.add(p4);
        chapter2.add(p5);
        chapter2.add(p6);
        chapter2.add(p7);
        chapter2.add(p8);
        return chapter2;
    }

    /**
     *@author: hewei
     *@Description: 设置请求表格
     */
    public void RequestTable( Chapter chapter2) throws DocumentException {
        //章节三  请求参数
        //项目简表
        Paragraph base = new Paragraph("\n请求参数", new RtfFont("仿宋", 14, Font.BOLD, Color.BLACK));
        chapter2.add(base);

        //表格
        Table baseTable = new Table(4);
        baseTable.setBorderColor(Color.BLACK);
        baseTable.setWidth(100);
        baseTable.setPadding(10);
        baseTable.setSpacing(0);
        //第一行
        Cell baseCell = new Cell(new Paragraph("名称", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("类型", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("必填", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("说明", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);


        //表格
        //第二行
        baseCell = new Cell(new Paragraph("", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseTable.addCell(baseCell);


        chapter2.add(baseTable);
    }

    /**
     *@author: hewei
     *@Description: 设置返回表格
     */
    public void ResponseTable( Chapter chapter2) throws DocumentException {
        //章节三  请求参数
        //项目简表
        Paragraph base = new Paragraph("\n返回参数", new RtfFont("仿宋", 14, Font.BOLD, Color.BLACK));
        chapter2.add(base);

        //表格
        Table baseTable = new Table(4);
        baseTable.setBorderColor(Color.BLACK);
        baseTable.setWidth(100);
        baseTable.setPadding(10);
        baseTable.setSpacing(0);
        //第一行
        Cell baseCell = new Cell(new Paragraph("名称", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("类型", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("必填", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        baseCell = new Cell(new Paragraph("说明", new RtfFont("宋提", 14, Font.NORMAL, Color.BLACK)));
        baseCell.setBorderColor(Color.black);
        baseCell.setBackgroundColor(Color.LIGHT_GRAY);
        baseTable.addCell(baseCell);
        chapter2.add(baseTable);
    }

    /**
     *@author: hewei
     *@Description: 设置示例
     */
    public void example(Chapter chapter2){
        Paragraph responseExample = new Paragraph("\n返回示例", new RtfFont("仿宋", 14, Font.BOLD, Color.BLACK));
        chapter2.add(responseExample);
        Paragraph correctResponseExample = new Paragraph("\n正确返回示例", new RtfFont("仿宋", 14, Font.BOLD, Color.BLACK));
        correctResponseExample.setIndentationLeft(20);
        chapter2.add(correctResponseExample);
        Paragraph errorResponseExample = new Paragraph("\n错误返回示例", new RtfFont("仿宋", 14, Font.BOLD, Color.BLACK));
        errorResponseExample.setIndentationLeft(20);
        chapter2.add(errorResponseExample);
    }

}
